package entity.units;

import java.awt.Graphics2D;

import loading.ImageLoader;
import math.Vector2D;
import entity.Attackable;
import entity.DynamicEntity;

public abstract class Unit extends DynamicEntity implements Attackable
{
	protected static final int STANDING = 0;
	protected static final int MOVING = 1;

	private int state;
	
	private double timeTraining;
	private double timeToTrain;

	private int health, totalHealth;	

	public Unit( Vector2D pos, ImageLoader img, double timeToTrain, int totalHealth )
	{
		super( pos, img );
		
		state = STANDING;
		
		this.timeToTrain = timeToTrain;
		this.timeTraining = 0;
		
		this.totalHealth = health = totalHealth;
	}
	
	public abstract void stop();
	public abstract void goTo( Vector2D end );

	public void update( double duration )
	{
		if( this.isEnabled() )
			super.update( duration );
	}
	
	public void draw( Graphics2D g )
	{
		if( this.isTrained() )
			super.draw( g );
	}
	
	public double getTimetoTrain()
	{
		return timeToTrain;
	}
	
	public double getTimeTraining()
	{
		return timeTraining;
	}
	
	public void incTimeTraining( double d )
	{
		timeTraining += d;
	}
	
	public boolean isTrained()
	{
		return timeTraining >= timeToTrain;
	}
	
	public void setAsTrained()
	{
		timeTraining = timeToTrain;
	}
	
	protected int getState()
	{
		return state;
	}

	protected void setState( int i )
	{
		state = i;
	}
	
	public boolean isEnabled()
	{
		return !isDead() && isTrained();
	}
	
	public boolean isDead()
	{
		return health <= 0;
	}

	public void damage( int amount )
	{
		health -= amount;
	}

	public void seek( Vector2D v )
	{
		Vector2D desired = Vector2D.multiply( Vector2D.getNormal( Vector2D.subtract( v, this.getPosition() ) ), getMaxSpeed() * 40 );
		
		this.setSteeringForce( Vector2D.subtract( desired, this.getVelocity() ) );
	}
}
